<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;
use app\common\model\SearchLog;
class Resource extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        if ($this->request->isAjax()) {
            $params = $this->request->post();
            $map['status'] = 'normal';
            $map['type_id'] = $params['type_id'];
            if($params['keywords']){
                $searchlog = SearchLog::get(['name'=>$params['keywords']]);
                if(empty($searchlog)){
                    $searchlog = new Searchlog();
                    $searchlog->name = $params['keywords'];
                    $searchlog->nums = 1;
                    
                }else{
                    $searchlog->nums = $searchlog['nums'] + 1;
                }
                $searchlog->save();
                $map['title|content'] = ['like','%'.$params['keywords'].'%'];
            }
            if(isset($params['item']) &&count($params['item'])>0){
                foreach ($params['item'] as $key => $value) {
                    if($value['value'] !='全部'){
                        $map['form'.($key+1)] = $value['value'];
                    }
                }
            }
            $list = model('Resource')->where($map)->page($params['page'],10)->select();
            foreach ($list as $key => $value) {
                $list[$key]['images'] = explode(',', $value['images']);
            }
            return json(['code'=>200,'data'=>$list,'msg'=>'success']);
        }
       $source =  Db::name('Resource')->where('status','normal')->order('createtime desc')->limit(10)->select();
       foreach ($source as $key => $value) {
           $source[$key]['image'] = explode(',', $value['images'])[0];
           $source[$key]['description'] = mb_substr(strip_tags($value['content']),0,30);
       }
       $this->assign('source',$source); 
       return $this->view->fetch();
    }
    
    public function detail(){
        if ($this->request->isAjax()) {
            $params = $this->request->post();
            $data['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
            ->where('d.type','wr')->where('d.doc_id',$params['id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
            $data['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
            ->where('d.type','wr')->where('d.doc_id',$params['id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->select();
            
            return json(['code'=>200,'data'=>$data,'msg'=>'success']);
        }
        $id = $this->request->get('id');
        $info = Db::name('Resource')->where('id',$id)->find();
        $info['type_name'] = Db::name('UserResource')->where('id',$info['type_id'])->value('name');
        $info['images'] = explode(',', $info['images']);
        $item = Db::name('ResourceItem')->where('type_id',$info['type_id'])->order('id asc')->select();
        $this->assign('item',$item);
        $this->assign('info',$info);
        $this->assign('id',$id);
        Db::name('Resource')->where('id',$id)->setInc('clicks',1);
        Db::name('Document')->where('doc_id',$id)->where('type','wr')->setInc('clicks',1);
        Db::name('Resource')->where('id',$id)->setInc('once_clicks',1);
        Db::name('Document')->where('doc_id',$id)->where('type','wr')->setInc('once_clicks',1);
        return $this->view->fetch();
    }

}
